<template>
  <div class="bg">
    <img class="Icon" src="../../svg/tangerine.svg" alt="" />
    <div class="title">橘子小说后台管理</div>
    <div class="fr">
      <el-form
        :model="user"
        :rules="rules"
        ref="userForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="user.username"
            placeholder="请输入账户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="user.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-button
          class="my-el-button login"
          type="primary"
          size="medium"
          @click="login"
          >登入</el-button
        >
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from '@/network/userManager'
export default {
  data() {
    return {
      user: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          { required: true, message: '请输入账户', trigger: 'blur' },
          { min: 5, max: 8, message: '长度在 5 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 8, message: '长度在 5 到 8 个字符', trigger: 'blur' }
        ]
      },
      fullscreenLoading: false
    }
  },
  methods: {
    // 登入方法
    login() {
      this.$refs['userForm'].validate(async valid => {
        if (valid) {
           const loading = this.$loading({
          lock: true,
          text: '登入中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
          let us = await login(this.user)
          console.info(us)
          if (us.errcode !== 0 || us.data.length === 0)
            return this.$message.error('账户或密码错误')
          us = JSON.parse(us.data[0])
          if (us.adStatus !== 1) return this.$message.error('该账户已冻结')
          console.info(us)
          sessionStorage.setItem('user', JSON.stringify(us))
           loading.close();
          this.$router.push('/')
        }
      })
    }
  },
  created() {
    var _self = this
    document.onkeydown = function() {
      var key = window.event.keyCode
      if (key == 13) {
        _self.login()
      }
    }
  }
}
</script>
<style>
.bg {
  position: absolute;
  width: 400px;
  height: 340px;
  left: 50%;
  top: 50%;
  margin-left: -200px;
  margin-top: -220px;
}
.title {
  font-weight: 700;
  text-align: center;
  color: #fd7c27;
  margin-bottom: 25px;
  font-size: 27px;
}
.fr {
  width: 350px;
  margin: 0px auto;
}

.login {
  width: 100%;
}
.Icon {
  display: inline-block;
  text-align: center;
  padding: 0px 127px;
}
</style>
